//对象参数处理成字符串类型
export function transParams(params) {
  if (typeof params === 'string' || !params) return params
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    let part = encodeURIComponent(propName) + '='
    if (value !== null && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            let params = propName + '[' + key + ']'
            let subPart = encodeURIComponent(params) + '='
            result += subPart + encodeURIComponent(value[key]) + '&'
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&'
      }
    }
  }
  if (result) result = result.slice(0, -1)
  return result
}

//获取url中参数
export function getQuery(url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}

// 返回项目路径
export function getNormalPath(p) {
  if (p.length === 0 || !p || p == 'undefined') {
    return p
  }
  let res = p.replace('//', '/')
  if (res[res.length - 1] === '/') {
    return res.slice(0, res.length - 1)
  }
  return res
}

// url 拼接域名
export function urlDomain(url, domain = '') {
  if (/^(https?:|mailto:|tel:)/.test(url)) return url
  if (!domain || !url) return url
  if (domain.charAt(domain.length - 1) === '/') domain = domain.slice(0, -1)
  if (url.indexOf('/') === 0) url = url.slice(1)
  return url ? domain + '/' + url : domain
}

// url拼接参数
export function urlParams(url, params = null) {
  if (!params || !url) return url
  params = transParams(params)
  if (!url) return params
  return url + (url.indexOf('?') > 0 ? '&' : '?') + params
}

// 获取文件名称
export function getFileName(path) {
  // 如果是url那么取最后的名字 如果不是直接返回
  if (path.lastIndexOf('/') > -1) {
    return path.slice(path.lastIndexOf('/') + 1)
  } else {
    return path
  }
}

// 获取文件后缀
export function getFileExt(path, isLower = false) {
  let ext = ''
  if (path.lastIndexOf('.') > -1) {
    ext = path.slice(path.lastIndexOf('.') + 1)
  }
  return isLower ? ext.toLowerCase() : ext
}

// 获取文件的类型图标和类型
export function getFileInfo(path) {
  const ext = getFileExt(path)
  let result = { icon: new URL('@/assets/images/file/unknown.png', import.meta.url).href, type: 'file' }

  if (['jpg', 'jpeg', 'png', 'gif'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/image.png', import.meta.url).href, type: 'image' }
  } else if (['mp4'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/video.png', import.meta.url).href, type: 'video' }
  } else if (['mp3'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/audio.png', import.meta.url).href, type: 'audio' }
  } else if (ext === 'pdf') {
    result = { icon: new URL('@/assets/images/file/pdf.png', import.meta.url).href, type: 'pdf' }
  } else if (ext === 'txt') {
    result = { icon: new URL('@/assets/images/file/txt.png', import.meta.url), type: 'txt' }
  } else if (['ppt', 'pptx'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/ppt.png', import.meta.url).href, type: 'ppt' }
  } else if (['xls', 'xlsx'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/excel.png', import.meta.url).href, type: 'excel' }
  } else if (['doc', 'docx'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/word.png', import.meta.url).href, type: 'word' }
  } else if (['rar', 'zip', '7z', 'tar', 'gz', 'iso'].includes(ext)) {
    result = { icon: new URL('@/assets/images/file/rar.png', import.meta.url).href, type: 'rar' }
  }
  return result
}
